package com.chaoyer.cynet.common.aspect;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.chaoyer.cynet.common.context.CyNetContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class AuthAspect {

    // 切点
    @Pointcut("execution(* com.chaoyer.cynet.controller.*.*(..))")
    public void point() {
    }


    @Around(value = "point()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        Object result;

        Object[] arg = pjp.getArgs();
        CyNetContext req = (CyNetContext) arg[0];

        // 日志处理
        log(req);


        result = pjp.proceed();

        return result;
    }

    // 打印请求日志
    private void log(CyNetContext ctx) {

        MDC.put("userId", "0");

        try {
            log.info("[RECV]: " + JSONObject.toJSONString(ctx));
        } catch (JSONException e) {
            log.info("[RECV]: " + JSONObject.toJSONString(ctx.toString()));
        }
    }
}
